import Vue from "vue";
import config from "@/utils/config.js"
import interfaces from '@/utils/interfaces.js';
const getters = {};
export default {
	getters,
	state: {
		timer: null, // 用于保存定时器ID
		countdownTime: 5 * 60 * 1000, // 5分钟倒计时
		userInfo: {},
		openid: '',
		openidSaveTime: 0,
		spread_id: 0,
		continuous: []
	},
	mutations: { //同步 也就是必须操作完成再进行下一步
		//commit的作用，就是调用store中某个mutation的函数。
		//修改操作state的数据，要通过mutations
		//更新video剧的信息
		mut_userInfo(state, infoObj) {
			if (typeof infoObj == 'object' && infoObj != null) {
				state.userInfo = infoObj;
				// vuex存进userInfo缓存
				uni.setStorageSync('userInfo', state.userInfo);
			}
		},
		mut_openid(state, openidString) {
			if (typeof openidString == 'string' && openidString) {
				state.openidSaveTime = Date.now();
				state.openid = openidString;
				uni.setStorageSync('openid', state.openid);
				uni.setStorageSync('openidSaveTime', state.openidSaveTime);
			}
		},
		mut_spread_id(state, spread_id) {
			if (typeof spread_id == 'number') {
				state.spread_id = spread_id;
				uni.setStorageSync('spread_id', state.spread_id);
			}
		},
		mut_continuous(state, cArr) {
			if (Array.isArray(cArr)) {
				if (cArr.length) {
					state.continuous = cArr;
					uni.setStorageSync('continuous', state.continuous);
				}
			}
		},
		mut_startCountDown(state,that) {
			console.log(50)
			if(!state.timer){
				console.warn('=========倒计时=========');
				let nowTime = Date.now(),
				endTime = uni.getStorageSync('countdownTime')||(nowTime+state.countdownTime);
				state.timer = setInterval(() => {
					console.log(5555555)
					if ( nowTime<endTime ) {
						endTime-=1000;
						uni.setStorageSync('countdownTime', endTime);
						console.warn('倒计时：'+endTime);
					} else {
						console.warn('倒计时结束：'+nowTime);
						clearInterval(state.timer);
						state.timer = null;
						//调用接口
						that.request({
							url: interfaces.ios_payment,
							method: config.post,
							data: {},
							success: (r) => {
								console.log('上传', r);
								// uni.showModal({
								// 	title: '提示',
								// 	content: '上传成功，解锁支付',
								// 	showCancel:false
								// });
								if (r.code === config.SUCCESS) {
									if(typeof r.content == 'object' && r.content!=null){
										if(JSON.stringify(r.content)!="{}"){
											that.$store.commit("mut_userInfo", r.content);
										}
									}
								}
							}
						})
					}
				
				}, 1000);
			}
		},
		// 清除定时器
		clearTimer(state) {
			clearInterval(state.timer);
			state.timer = null;
		}
	},
	actions: { //异步 
		//也就是不管操作是否完成，都是立即去下一步
		//dispatch的作用，专门用来触发action
		//初始化获取用户openid
		act_openid({
			state,
			dispatch
		}) {
			console.log('初始化获取用户openid');
			state.openid = uni.getStorageSync('openid') || '';
			state.openidSaveTime = uni.getStorageSync('openidSaveTime') || 0;
		},
		//初始化获取spread_id
		act_spread_id({
			state,
			dispatch
		}) {
			console.log('初始化获取spread_id');
			state.spread_id = uni.getStorageSync('spread_id') || 0;
		},
		//初始化获取用户信息
		act_userInfo({
			state,
			dispatch
		}) {
			console.log('初始化获取用户信息');
			state.userInfo = uni.getStorageSync('userInfo') || {};
		},
	}
}